package com.chat.api.web.service;

import com.chat.api.utils.ResultUtil;
import com.chat.api.utils.util.IdWorkerUtil;
import com.chat.api.utils.util.ParamUtil;
import com.chat.api.web.dao.LikeRepo;
import com.chat.api.web.po.LikeBean;
import com.chat.api.web.po.UserBean;
import com.chat.api.web.vo.MessageEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class LikeService {

    @Autowired
    private LikeRepo likeRepo;
    @Autowired
    private UserService userService;

    /**
     * 获取点赞数量、判断user是否已点赞
     * @param blogId
     * @param request
     * @return
     */
    public MessageEntity getCount(Long blogId, HttpServletRequest request) {
        try {
            Map<String, Object> map = new HashMap<>();
            Integer isLike = 0;
            HttpSession session = request.getSession();
            String userId = (String) session.getAttribute(session.getId());
            Long userIdL = ParamUtil.getLongParameter(userId, -1L);
            Integer count = likeRepo.findCountByBlogId(blogId);
            LikeBean like = likeRepo.findLike(userIdL, blogId);
            if (like != null){
                isLike = like.getIsLike();
            }
            map.put("isLike",isLike);
            map.put("count",count);
            return ResultUtil.success(map);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.fail("获取点赞数量失败！！！");
        }
    }

    /**
     * 点赞or取消赞
     * @param blogId
     * @param request
     * @return
     */
    @Transactional(readOnly = false)
    public MessageEntity isLike(Long blogId, HttpServletRequest request){
        try {
            HttpSession session = request.getSession();
            String userId = (String) session.getAttribute(session.getId());
            Long userIdL = ParamUtil.getLongParameter(userId, -1L);
            LikeBean bean = likeRepo.findLike(userIdL,blogId);
            if (bean == null){
                bean = new LikeBean();
                Long id = IdWorkerUtil.getInstance().nextId();
                bean.setId(id);
                bean.setUserId(userIdL);
                bean.setBlogId(blogId);
                bean.setIsLike(1);
                bean.setCreateBy(userIdL);
                bean.setCreateDate(new Date());
                bean.setUpdateDate(new Date());
                bean.setUpdateBy(userIdL);
            }else {
                Integer isLike = bean.getIsLike();
                if (isLike == 0) bean.setIsLike(1);
                else bean.setIsLike(0);
            }
            likeRepo.save(bean);
            return ResultUtil.success(bean);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.fail("系统繁忙，请稍后再试！！！");
        }
    }

}
